package minefak3r;

/**
 *
 * @author Chriss & D4rky!
 */
import java.io.InputStream;
import java.util.Scanner;

public class ConsoleReader extends Thread {
    
    Scanner readLines;
    String systemFlag;
    SystemPrintStream sp;
    ServerList sl;
    
    public ConsoleReader(String systemFlag, InputStream consoleInput, SystemPrintStream sp) throws Exception {
        this.readLines = new Scanner(consoleInput);
        this.systemFlag = systemFlag;
        this.sp = sp;
        this.sl = new ServerList("ServerList.txt");
    }
    
    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(1);
                if (readLines.hasNextLine()) {
                    String command = readLines.nextLine();
                    if (sp.l != null) {
                        sp.l.Log("->" + command);
                    }
                    if (!command.contains(systemFlag)) {
                        execute(command);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
    
    public void execute(String command) throws Exception {
        if (command.contains("/ip")) {
            char exec[] = command.toCharArray();
            int start = 0;
            
            for (int z = 0; z < exec.length; z++) {
                if (String.valueOf(exec[z]).equals(" ")) {
                    start = z + 1;
                }
            }
            String ip = command.substring(start, command.length());
            sp.SystemOut(systemFlag + ip);
            Main.targetPort = Utils.getPort(ip);
            Main.targetIP = Utils.getIP(ip);
        } else if (command.contains("/exit")) {
            sp.SystemOut(systemFlag + "The Server will be closed soon ...");
            System.exit(0);
        } else if (command.contains("/log")) {
            sp.initLogger(command.substring(5));
            sp.SystemOut(systemFlag + "The following output will be logged...");
        } else if (command.contains("/add")) {
            sl.addServer(command.substring(5));
            sp.SystemOut(systemFlag + "< " + command.substring(5) + " > " + "is added to the ServerList ...");
        } else if (command.contains("/list")) {
            sp.SystemOut(sl.getServer());
        } else if (command.contains("/select")) {
            char exec[] = command.toCharArray();
            int start = 0;
            
            for (int z = 0; z < exec.length; z++) {
                if (String.valueOf(exec[z]).equals(" ")) {
                    start = z + 1;
                }
            }
            int value = Integer.valueOf(command.substring(start, command.length()));
            sp.SystemOut("< " + sl.getServerAdress(value) + " >" + " is your selected Server ...");
            Main.targetPort = Utils.getPort(sl.getServerAdress(value));
            Main.targetIP = Utils.getIP(sl.getServerAdress(value));
        } else if (command.contains("/drunk")) {
            if (!Player.getDrunk()) {
                Player.changeDrunk(true);
            } else {
                Player.changeDrunk(false);                
            }
        } else {
            sp.SystemOut(systemFlag + "The command <" + command + "> doesn't exist ...");
        }
    }
}
